from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from .serializers import CategorySerializer, NewsDetailSerializer, NewsListSerializer, SubCommentSerializer
from .models import News, Comment, Category


class CategoryView(ListAPIView):
    serializer_class = CategorySerializer
    queryset = Category.objects.all().order_by('id')


class NewsViewset(ReadOnlyModelViewSet):
    queryset = News.objects.filter(status=1).order_by('-create_time')

    def get_serializer_class(self):
        if self.action == 'list':
            return NewsListSerializer

        else:
            return NewsDetailSerializer

class CommentView(ListAPIView):
    serializer_class = SubCommentSerializer
    def get_queryset(self):
        news_id = self.kwargs['news_id']
        queryset = Comment.objects.filter(news_id = news_id).order_by('-create_time')
        return queryset